// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"server/components/models/ent/region"
	"server/components/models/ent/regiontype"
	"server/components/models/ent/schema"
	"server/components/models/ent/user"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// RegionCreate is the builder for creating a Region entity.
type RegionCreate struct {
	config
	mutation *RegionMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetName sets the "name" field.
func (rc *RegionCreate) SetName(s string) *RegionCreate {
	rc.mutation.SetName(s)
	return rc
}

// SetParentRegion sets the "parentRegion" field.
func (rc *RegionCreate) SetParentRegion(s string) *RegionCreate {
	rc.mutation.SetParentRegion(s)
	return rc
}

// SetNillableParentRegion sets the "parentRegion" field if the given value is not nil.
func (rc *RegionCreate) SetNillableParentRegion(s *string) *RegionCreate {
	if s != nil {
		rc.SetParentRegion(*s)
	}
	return rc
}

// SetLatitude1 sets the "latitude1" field.
func (rc *RegionCreate) SetLatitude1(f float64) *RegionCreate {
	rc.mutation.SetLatitude1(f)
	return rc
}

// SetNillableLatitude1 sets the "latitude1" field if the given value is not nil.
func (rc *RegionCreate) SetNillableLatitude1(f *float64) *RegionCreate {
	if f != nil {
		rc.SetLatitude1(*f)
	}
	return rc
}

// SetLongitude1 sets the "longitude1" field.
func (rc *RegionCreate) SetLongitude1(f float64) *RegionCreate {
	rc.mutation.SetLongitude1(f)
	return rc
}

// SetNillableLongitude1 sets the "longitude1" field if the given value is not nil.
func (rc *RegionCreate) SetNillableLongitude1(f *float64) *RegionCreate {
	if f != nil {
		rc.SetLongitude1(*f)
	}
	return rc
}

// SetLatitude2 sets the "latitude2" field.
func (rc *RegionCreate) SetLatitude2(f float64) *RegionCreate {
	rc.mutation.SetLatitude2(f)
	return rc
}

// SetNillableLatitude2 sets the "latitude2" field if the given value is not nil.
func (rc *RegionCreate) SetNillableLatitude2(f *float64) *RegionCreate {
	if f != nil {
		rc.SetLatitude2(*f)
	}
	return rc
}

// SetLongitude2 sets the "longitude2" field.
func (rc *RegionCreate) SetLongitude2(f float64) *RegionCreate {
	rc.mutation.SetLongitude2(f)
	return rc
}

// SetNillableLongitude2 sets the "longitude2" field if the given value is not nil.
func (rc *RegionCreate) SetNillableLongitude2(f *float64) *RegionCreate {
	if f != nil {
		rc.SetLongitude2(*f)
	}
	return rc
}

// SetOsmData sets the "osmData" field.
func (rc *RegionCreate) SetOsmData(sd schema.OsmData) *RegionCreate {
	rc.mutation.SetOsmData(sd)
	return rc
}

// SetNillableOsmData sets the "osmData" field if the given value is not nil.
func (rc *RegionCreate) SetNillableOsmData(sd *schema.OsmData) *RegionCreate {
	if sd != nil {
		rc.SetOsmData(*sd)
	}
	return rc
}

// SetID sets the "id" field.
func (rc *RegionCreate) SetID(i int64) *RegionCreate {
	rc.mutation.SetID(i)
	return rc
}

// SetRegionTypeID sets the "regionType" edge to the RegionType entity by ID.
func (rc *RegionCreate) SetRegionTypeID(id int64) *RegionCreate {
	rc.mutation.SetRegionTypeID(id)
	return rc
}

// SetNillableRegionTypeID sets the "regionType" edge to the RegionType entity by ID if the given value is not nil.
func (rc *RegionCreate) SetNillableRegionTypeID(id *int64) *RegionCreate {
	if id != nil {
		rc = rc.SetRegionTypeID(*id)
	}
	return rc
}

// SetRegionType sets the "regionType" edge to the RegionType entity.
func (rc *RegionCreate) SetRegionType(r *RegionType) *RegionCreate {
	return rc.SetRegionTypeID(r.ID)
}

// SetAccountIdID sets the "accountId" edge to the User entity by ID.
func (rc *RegionCreate) SetAccountIdID(id int) *RegionCreate {
	rc.mutation.SetAccountIdID(id)
	return rc
}

// SetNillableAccountIdID sets the "accountId" edge to the User entity by ID if the given value is not nil.
func (rc *RegionCreate) SetNillableAccountIdID(id *int) *RegionCreate {
	if id != nil {
		rc = rc.SetAccountIdID(*id)
	}
	return rc
}

// SetAccountId sets the "accountId" edge to the User entity.
func (rc *RegionCreate) SetAccountId(u *User) *RegionCreate {
	return rc.SetAccountIdID(u.ID)
}

// Mutation returns the RegionMutation object of the builder.
func (rc *RegionCreate) Mutation() *RegionMutation {
	return rc.mutation
}

// Save creates the Region in the database.
func (rc *RegionCreate) Save(ctx context.Context) (*Region, error) {
	return withHooks(ctx, rc.sqlSave, rc.mutation, rc.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (rc *RegionCreate) SaveX(ctx context.Context) *Region {
	v, err := rc.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (rc *RegionCreate) Exec(ctx context.Context) error {
	_, err := rc.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rc *RegionCreate) ExecX(ctx context.Context) {
	if err := rc.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (rc *RegionCreate) check() error {
	if _, ok := rc.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Region.name"`)}
	}
	return nil
}

func (rc *RegionCreate) sqlSave(ctx context.Context) (*Region, error) {
	if err := rc.check(); err != nil {
		return nil, err
	}
	_node, _spec := rc.createSpec()
	if err := sqlgraph.CreateNode(ctx, rc.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = int64(id)
	}
	rc.mutation.id = &_node.ID
	rc.mutation.done = true
	return _node, nil
}

func (rc *RegionCreate) createSpec() (*Region, *sqlgraph.CreateSpec) {
	var (
		_node = &Region{config: rc.config}
		_spec = sqlgraph.NewCreateSpec(region.Table, sqlgraph.NewFieldSpec(region.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = rc.conflict
	if id, ok := rc.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := rc.mutation.Name(); ok {
		_spec.SetField(region.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := rc.mutation.ParentRegion(); ok {
		_spec.SetField(region.FieldParentRegion, field.TypeString, value)
		_node.ParentRegion = value
	}
	if value, ok := rc.mutation.Latitude1(); ok {
		_spec.SetField(region.FieldLatitude1, field.TypeFloat64, value)
		_node.Latitude1 = value
	}
	if value, ok := rc.mutation.Longitude1(); ok {
		_spec.SetField(region.FieldLongitude1, field.TypeFloat64, value)
		_node.Longitude1 = value
	}
	if value, ok := rc.mutation.Latitude2(); ok {
		_spec.SetField(region.FieldLatitude2, field.TypeFloat64, value)
		_node.Latitude2 = value
	}
	if value, ok := rc.mutation.Longitude2(); ok {
		_spec.SetField(region.FieldLongitude2, field.TypeFloat64, value)
		_node.Longitude2 = value
	}
	if value, ok := rc.mutation.OsmData(); ok {
		_spec.SetField(region.FieldOsmData, field.TypeJSON, value)
		_node.OsmData = value
	}
	if nodes := rc.mutation.RegionTypeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   region.RegionTypeTable,
			Columns: []string{region.RegionTypeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(regiontype.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.region_region_type = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := rc.mutation.AccountIdIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   region.AccountIdTable,
			Columns: []string{region.AccountIdColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.region_account_id = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Region.Create().
//		SetName(v).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.RegionUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (rc *RegionCreate) OnConflict(opts ...sql.ConflictOption) *RegionUpsertOne {
	rc.conflict = opts
	return &RegionUpsertOne{
		create: rc,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.Region.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (rc *RegionCreate) OnConflictColumns(columns ...string) *RegionUpsertOne {
	rc.conflict = append(rc.conflict, sql.ConflictColumns(columns...))
	return &RegionUpsertOne{
		create: rc,
	}
}

type (
	// RegionUpsertOne is the builder for "upsert"-ing
	//  one Region node.
	RegionUpsertOne struct {
		create *RegionCreate
	}

	// RegionUpsert is the "OnConflict" setter.
	RegionUpsert struct {
		*sql.UpdateSet
	}
)

// SetName sets the "name" field.
func (u *RegionUpsert) SetName(v string) *RegionUpsert {
	u.Set(region.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *RegionUpsert) UpdateName() *RegionUpsert {
	u.SetExcluded(region.FieldName)
	return u
}

// SetParentRegion sets the "parentRegion" field.
func (u *RegionUpsert) SetParentRegion(v string) *RegionUpsert {
	u.Set(region.FieldParentRegion, v)
	return u
}

// UpdateParentRegion sets the "parentRegion" field to the value that was provided on create.
func (u *RegionUpsert) UpdateParentRegion() *RegionUpsert {
	u.SetExcluded(region.FieldParentRegion)
	return u
}

// ClearParentRegion clears the value of the "parentRegion" field.
func (u *RegionUpsert) ClearParentRegion() *RegionUpsert {
	u.SetNull(region.FieldParentRegion)
	return u
}

// SetLatitude1 sets the "latitude1" field.
func (u *RegionUpsert) SetLatitude1(v float64) *RegionUpsert {
	u.Set(region.FieldLatitude1, v)
	return u
}

// UpdateLatitude1 sets the "latitude1" field to the value that was provided on create.
func (u *RegionUpsert) UpdateLatitude1() *RegionUpsert {
	u.SetExcluded(region.FieldLatitude1)
	return u
}

// AddLatitude1 adds v to the "latitude1" field.
func (u *RegionUpsert) AddLatitude1(v float64) *RegionUpsert {
	u.Add(region.FieldLatitude1, v)
	return u
}

// ClearLatitude1 clears the value of the "latitude1" field.
func (u *RegionUpsert) ClearLatitude1() *RegionUpsert {
	u.SetNull(region.FieldLatitude1)
	return u
}

// SetLongitude1 sets the "longitude1" field.
func (u *RegionUpsert) SetLongitude1(v float64) *RegionUpsert {
	u.Set(region.FieldLongitude1, v)
	return u
}

// UpdateLongitude1 sets the "longitude1" field to the value that was provided on create.
func (u *RegionUpsert) UpdateLongitude1() *RegionUpsert {
	u.SetExcluded(region.FieldLongitude1)
	return u
}

// AddLongitude1 adds v to the "longitude1" field.
func (u *RegionUpsert) AddLongitude1(v float64) *RegionUpsert {
	u.Add(region.FieldLongitude1, v)
	return u
}

// ClearLongitude1 clears the value of the "longitude1" field.
func (u *RegionUpsert) ClearLongitude1() *RegionUpsert {
	u.SetNull(region.FieldLongitude1)
	return u
}

// SetLatitude2 sets the "latitude2" field.
func (u *RegionUpsert) SetLatitude2(v float64) *RegionUpsert {
	u.Set(region.FieldLatitude2, v)
	return u
}

// UpdateLatitude2 sets the "latitude2" field to the value that was provided on create.
func (u *RegionUpsert) UpdateLatitude2() *RegionUpsert {
	u.SetExcluded(region.FieldLatitude2)
	return u
}

// AddLatitude2 adds v to the "latitude2" field.
func (u *RegionUpsert) AddLatitude2(v float64) *RegionUpsert {
	u.Add(region.FieldLatitude2, v)
	return u
}

// ClearLatitude2 clears the value of the "latitude2" field.
func (u *RegionUpsert) ClearLatitude2() *RegionUpsert {
	u.SetNull(region.FieldLatitude2)
	return u
}

// SetLongitude2 sets the "longitude2" field.
func (u *RegionUpsert) SetLongitude2(v float64) *RegionUpsert {
	u.Set(region.FieldLongitude2, v)
	return u
}

// UpdateLongitude2 sets the "longitude2" field to the value that was provided on create.
func (u *RegionUpsert) UpdateLongitude2() *RegionUpsert {
	u.SetExcluded(region.FieldLongitude2)
	return u
}

// AddLongitude2 adds v to the "longitude2" field.
func (u *RegionUpsert) AddLongitude2(v float64) *RegionUpsert {
	u.Add(region.FieldLongitude2, v)
	return u
}

// ClearLongitude2 clears the value of the "longitude2" field.
func (u *RegionUpsert) ClearLongitude2() *RegionUpsert {
	u.SetNull(region.FieldLongitude2)
	return u
}

// SetOsmData sets the "osmData" field.
func (u *RegionUpsert) SetOsmData(v schema.OsmData) *RegionUpsert {
	u.Set(region.FieldOsmData, v)
	return u
}

// UpdateOsmData sets the "osmData" field to the value that was provided on create.
func (u *RegionUpsert) UpdateOsmData() *RegionUpsert {
	u.SetExcluded(region.FieldOsmData)
	return u
}

// ClearOsmData clears the value of the "osmData" field.
func (u *RegionUpsert) ClearOsmData() *RegionUpsert {
	u.SetNull(region.FieldOsmData)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
//	client.Region.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(region.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *RegionUpsertOne) UpdateNewValues() *RegionUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.ID(); exists {
			s.SetIgnore(region.FieldID)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Region.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *RegionUpsertOne) Ignore() *RegionUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *RegionUpsertOne) DoNothing() *RegionUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the RegionCreate.OnConflict
// documentation for more info.
func (u *RegionUpsertOne) Update(set func(*RegionUpsert)) *RegionUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&RegionUpsert{UpdateSet: update})
	}))
	return u
}

// SetName sets the "name" field.
func (u *RegionUpsertOne) SetName(v string) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateName() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateName()
	})
}

// SetParentRegion sets the "parentRegion" field.
func (u *RegionUpsertOne) SetParentRegion(v string) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetParentRegion(v)
	})
}

// UpdateParentRegion sets the "parentRegion" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateParentRegion() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateParentRegion()
	})
}

// ClearParentRegion clears the value of the "parentRegion" field.
func (u *RegionUpsertOne) ClearParentRegion() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearParentRegion()
	})
}

// SetLatitude1 sets the "latitude1" field.
func (u *RegionUpsertOne) SetLatitude1(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetLatitude1(v)
	})
}

// AddLatitude1 adds v to the "latitude1" field.
func (u *RegionUpsertOne) AddLatitude1(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.AddLatitude1(v)
	})
}

// UpdateLatitude1 sets the "latitude1" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateLatitude1() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLatitude1()
	})
}

// ClearLatitude1 clears the value of the "latitude1" field.
func (u *RegionUpsertOne) ClearLatitude1() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLatitude1()
	})
}

// SetLongitude1 sets the "longitude1" field.
func (u *RegionUpsertOne) SetLongitude1(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetLongitude1(v)
	})
}

// AddLongitude1 adds v to the "longitude1" field.
func (u *RegionUpsertOne) AddLongitude1(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.AddLongitude1(v)
	})
}

// UpdateLongitude1 sets the "longitude1" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateLongitude1() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLongitude1()
	})
}

// ClearLongitude1 clears the value of the "longitude1" field.
func (u *RegionUpsertOne) ClearLongitude1() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLongitude1()
	})
}

// SetLatitude2 sets the "latitude2" field.
func (u *RegionUpsertOne) SetLatitude2(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetLatitude2(v)
	})
}

// AddLatitude2 adds v to the "latitude2" field.
func (u *RegionUpsertOne) AddLatitude2(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.AddLatitude2(v)
	})
}

// UpdateLatitude2 sets the "latitude2" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateLatitude2() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLatitude2()
	})
}

// ClearLatitude2 clears the value of the "latitude2" field.
func (u *RegionUpsertOne) ClearLatitude2() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLatitude2()
	})
}

// SetLongitude2 sets the "longitude2" field.
func (u *RegionUpsertOne) SetLongitude2(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetLongitude2(v)
	})
}

// AddLongitude2 adds v to the "longitude2" field.
func (u *RegionUpsertOne) AddLongitude2(v float64) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.AddLongitude2(v)
	})
}

// UpdateLongitude2 sets the "longitude2" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateLongitude2() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLongitude2()
	})
}

// ClearLongitude2 clears the value of the "longitude2" field.
func (u *RegionUpsertOne) ClearLongitude2() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLongitude2()
	})
}

// SetOsmData sets the "osmData" field.
func (u *RegionUpsertOne) SetOsmData(v schema.OsmData) *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.SetOsmData(v)
	})
}

// UpdateOsmData sets the "osmData" field to the value that was provided on create.
func (u *RegionUpsertOne) UpdateOsmData() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateOsmData()
	})
}

// ClearOsmData clears the value of the "osmData" field.
func (u *RegionUpsertOne) ClearOsmData() *RegionUpsertOne {
	return u.Update(func(s *RegionUpsert) {
		s.ClearOsmData()
	})
}

// Exec executes the query.
func (u *RegionUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for RegionCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *RegionUpsertOne) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}

// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *RegionUpsertOne) ID(ctx context.Context) (id int64, err error) {
	node, err := u.create.Save(ctx)
	if err != nil {
		return id, err
	}
	return node.ID, nil
}

// IDX is like ID, but panics if an error occurs.
func (u *RegionUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// RegionCreateBulk is the builder for creating many Region entities in bulk.
type RegionCreateBulk struct {
	config
	err      error
	builders []*RegionCreate
	conflict []sql.ConflictOption
}

// Save creates the Region entities in the database.
func (rcb *RegionCreateBulk) Save(ctx context.Context) ([]*Region, error) {
	if rcb.err != nil {
		return nil, rcb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(rcb.builders))
	nodes := make([]*Region, len(rcb.builders))
	mutators := make([]Mutator, len(rcb.builders))
	for i := range rcb.builders {
		func(i int, root context.Context) {
			builder := rcb.builders[i]
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*RegionMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, rcb.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					spec.OnConflict = rcb.conflict
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, rcb.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, rcb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (rcb *RegionCreateBulk) SaveX(ctx context.Context) []*Region {
	v, err := rcb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (rcb *RegionCreateBulk) Exec(ctx context.Context) error {
	_, err := rcb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (rcb *RegionCreateBulk) ExecX(ctx context.Context) {
	if err := rcb.Exec(ctx); err != nil {
		panic(err)
	}
}

// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
//	client.Region.CreateBulk(builders...).
//		OnConflict(
//			// Update the row with the new values
//			// the was proposed for insertion.
//			sql.ResolveWithNewValues(),
//		).
//		// Override some of the fields with custom
//		// update values.
//		Update(func(u *ent.RegionUpsert) {
//			SetName(v+v).
//		}).
//		Exec(ctx)
func (rcb *RegionCreateBulk) OnConflict(opts ...sql.ConflictOption) *RegionUpsertBulk {
	rcb.conflict = opts
	return &RegionUpsertBulk{
		create: rcb,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.Region.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (rcb *RegionCreateBulk) OnConflictColumns(columns ...string) *RegionUpsertBulk {
	rcb.conflict = append(rcb.conflict, sql.ConflictColumns(columns...))
	return &RegionUpsertBulk{
		create: rcb,
	}
}

// RegionUpsertBulk is the builder for "upsert"-ing
// a bulk of Region nodes.
type RegionUpsertBulk struct {
	create *RegionCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.Region.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//			sql.ResolveWith(func(u *sql.UpdateSet) {
//				u.SetIgnore(region.FieldID)
//			}),
//		).
//		Exec(ctx)
func (u *RegionUpsertBulk) UpdateNewValues() *RegionUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(region.FieldID)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.Region.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *RegionUpsertBulk) Ignore() *RegionUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
	return u
}

// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *RegionUpsertBulk) DoNothing() *RegionUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the RegionCreateBulk.OnConflict
// documentation for more info.
func (u *RegionUpsertBulk) Update(set func(*RegionUpsert)) *RegionUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&RegionUpsert{UpdateSet: update})
	}))
	return u
}

// SetName sets the "name" field.
func (u *RegionUpsertBulk) SetName(v string) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateName() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateName()
	})
}

// SetParentRegion sets the "parentRegion" field.
func (u *RegionUpsertBulk) SetParentRegion(v string) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetParentRegion(v)
	})
}

// UpdateParentRegion sets the "parentRegion" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateParentRegion() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateParentRegion()
	})
}

// ClearParentRegion clears the value of the "parentRegion" field.
func (u *RegionUpsertBulk) ClearParentRegion() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearParentRegion()
	})
}

// SetLatitude1 sets the "latitude1" field.
func (u *RegionUpsertBulk) SetLatitude1(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetLatitude1(v)
	})
}

// AddLatitude1 adds v to the "latitude1" field.
func (u *RegionUpsertBulk) AddLatitude1(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.AddLatitude1(v)
	})
}

// UpdateLatitude1 sets the "latitude1" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateLatitude1() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLatitude1()
	})
}

// ClearLatitude1 clears the value of the "latitude1" field.
func (u *RegionUpsertBulk) ClearLatitude1() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLatitude1()
	})
}

// SetLongitude1 sets the "longitude1" field.
func (u *RegionUpsertBulk) SetLongitude1(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetLongitude1(v)
	})
}

// AddLongitude1 adds v to the "longitude1" field.
func (u *RegionUpsertBulk) AddLongitude1(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.AddLongitude1(v)
	})
}

// UpdateLongitude1 sets the "longitude1" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateLongitude1() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLongitude1()
	})
}

// ClearLongitude1 clears the value of the "longitude1" field.
func (u *RegionUpsertBulk) ClearLongitude1() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLongitude1()
	})
}

// SetLatitude2 sets the "latitude2" field.
func (u *RegionUpsertBulk) SetLatitude2(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetLatitude2(v)
	})
}

// AddLatitude2 adds v to the "latitude2" field.
func (u *RegionUpsertBulk) AddLatitude2(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.AddLatitude2(v)
	})
}

// UpdateLatitude2 sets the "latitude2" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateLatitude2() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLatitude2()
	})
}

// ClearLatitude2 clears the value of the "latitude2" field.
func (u *RegionUpsertBulk) ClearLatitude2() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLatitude2()
	})
}

// SetLongitude2 sets the "longitude2" field.
func (u *RegionUpsertBulk) SetLongitude2(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetLongitude2(v)
	})
}

// AddLongitude2 adds v to the "longitude2" field.
func (u *RegionUpsertBulk) AddLongitude2(v float64) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.AddLongitude2(v)
	})
}

// UpdateLongitude2 sets the "longitude2" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateLongitude2() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateLongitude2()
	})
}

// ClearLongitude2 clears the value of the "longitude2" field.
func (u *RegionUpsertBulk) ClearLongitude2() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearLongitude2()
	})
}

// SetOsmData sets the "osmData" field.
func (u *RegionUpsertBulk) SetOsmData(v schema.OsmData) *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.SetOsmData(v)
	})
}

// UpdateOsmData sets the "osmData" field to the value that was provided on create.
func (u *RegionUpsertBulk) UpdateOsmData() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.UpdateOsmData()
	})
}

// ClearOsmData clears the value of the "osmData" field.
func (u *RegionUpsertBulk) ClearOsmData() *RegionUpsertBulk {
	return u.Update(func(s *RegionUpsert) {
		s.ClearOsmData()
	})
}

// Exec executes the query.
func (u *RegionUpsertBulk) Exec(ctx context.Context) error {
	if u.create.err != nil {
		return u.create.err
	}
	for i, b := range u.create.builders {
		if len(b.conflict) != 0 {
			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the RegionCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for RegionCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *RegionUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
